import grails.util.Environment
import groovy.time.TimeCategory

import org.apache.commons.logging.LogFactory

import com.carmanagement.FullTank
import com.carmanagement.Maintenance
import com.carmanagement.Role
import com.carmanagement.RoleEnum
import com.carmanagement.User
import com.carmanagement.UserRole
import com.carmanagement.Vehicle

class BootStrap {

    private static final log = LogFactory.getLog(this)

    def init = { servletContext ->

	if (!Role.findByAuthority(RoleEnum.ADMIN_ROLE.value)){
		def adminRole = new Role(authority: RoleEnum.ADMIN_ROLE.value).save(flush: true)
		def adminUser = new User(username: 'admin', password: '01-<k#o1X:p1<*I').save(flush: true)
		new UserRole(user:adminUser, role:adminRole).save(flush : true)
	}
		
	switch (Environment.current) {
	    case Environment.DEVELOPMENT:

	    //security
		def userRole = new Role(authority: RoleEnum.USER_ROLE.value).save(flush: true)		
		def toto = new User(username: 'toto', password: 'toto')
		def tata = new User(username: 'tata', password: 'tata')
		toto.save flush:true
		tata.save flush:true

		
		new UserRole(user:toto, role:userRole).save(flush : true)
		new UserRole(user:tata, role:userRole).save(flush : true)

		log.debug "create vehicle"
		def vehicle = new Vehicle(registerNumber: "test", price: 6000.00f, type: "Peugeot 1007", kilometers: 82000, user:toto  )

		vehicle.save()

		log.debug "create fullTank"

		def fullTank = new FullTank(quantity: 36.77f)
		fullTank.cost = 54.75f
		fullTank.date = Date.parse("yyyy/MM/dd", "2014/03/21")
		fullTank.mileage = 560i
		fullTank.vehicle = vehicle
		fullTank.save()

		fullTank = new FullTank(quantity: 36.97f)
		fullTank.cost = 55.6f
		fullTank.date = Date.parse("yyyy/MM/dd", "2014/04/02")
		fullTank.mileage = 515
		fullTank.vehicle = vehicle
		fullTank.save()

		fullTank = new FullTank(quantity: 36.32f)
		fullTank.cost = 54.95f
		fullTank.date = Date.parse("yyyy/MM/dd", "2014/04/13")
		fullTank.mileage = 492.3
		fullTank.vehicle = vehicle
		fullTank.save()

		fullTank = new FullTank(quantity: 37.11f)
		fullTank.cost = 56.15f
		fullTank.date = Date.parse("yyyy/MM/dd", "2014/04/21")
		fullTank.mileage = 546.7
		fullTank.vehicle = vehicle
		fullTank.save()

		fullTank = new FullTank(quantity: 36.80f)
		fullTank.cost = 55.75f
		fullTank.date = Date.parse("yyyy/MM/dd", "2014/05/09")
		fullTank.mileage = 525.0
		fullTank.vehicle = vehicle
		fullTank.save()

		fullTank = new FullTank(quantity: 35.41f)
		fullTank.cost = 53.65f
		fullTank.date = Date.parse("yyyy/MM/dd", "2014/05/14")
		fullTank.mileage = 500.8
		fullTank.vehicle = vehicle
		fullTank.save()

		Maintenance m = new Maintenance(description:"Simple test", predictedDate:new Date())
		m.cost = 54.75f
		m.date = new Date()
		m.mileage = 560i
		m.vehicle = vehicle
		m.save()

		vehicle = new Vehicle(registerNumber: "test2", price: 6000.00f, type: "Peugeot 1007", kilometers: 82000, user:tata  )

		vehicle.save()

		Random ramdom = new Random()
		for(i in 1..20){
		    fullTank = new FullTank(quantity: (ramdom.nextFloat() + ramdom.nextInt( 20 )))
		    fullTank.cost = ramdom.nextInt( 20 )
		    use (TimeCategory) {
				fullTank.date = new Date() + i.month
		    }
		    fullTank.mileage = 492.3
		    fullTank.vehicle = vehicle
		    fullTank.save()

		    m = new Maintenance(description:"Simple test", predictedDate:new Date())
		    use (TimeCategory) {
				m.predictedDate = i.month.from.now
				m.date = i.week.from.now
		    }
		    
		    m.cost = ramdom.nextInt( 50 )
		    m.mileage = 560i
		    m.vehicle = vehicle
		    m.save()
		}

	    default: break
	}

	log.debug "bootStrap end"
    }
    def destroy = {
    }
}
